<?php

namespace app\index\validate;

use app\index\model\CertPersonModel;
use app\index\service\OptService;
use think\Validate;

class CertEmploy extends Validate
{

    protected $rule = [
        'id' => 'require|number',
        'personid' => 'require|number|check_personid',
        'qualification' => 'require|check_qualification',
        'money' => 'require|check_money',
        'pay_type' => 'require|chekc_pay_type',
        'start_dt' => 'require|date',
        'end_dt' => 'require|date',
        'channel' => 'check_channel',
        'purpose' => 'check_purpose',
        'person_nature' => 'require'
    ];

    protected $message = [
        'id' => '网络错误',
        'personid' => '请选择持证人',
        'qualification' => '请选择聘用资格',
        'money' => '请填写年聘费',
        'pay_type' => '请选择聘费支付方式',
        'start_dt' => '请填写聘费开始时间',
        'end_dt' => '请填写聘费结束时间',
        'person_nature' => '请选择人员性质'
    ];

    protected $scene = [
        'add' => ['personid','qualification','money','pay_type','start_dt','end_dt','channel','purpose','person_nature'],
        'edit' => ['id','personid','qualification','money','pay_type','start_dt','end_dt','channel','purpose','person_nature'],
    ];

    public function check_personid($value){
        if(!CertPersonModel::where(['id'=>$value,'deleted_user'=>0])->find()){
            return '持证人不存在，请重新选择';
        }
        return true;
    }

    public function check_qualification($value){
        $list = array_filter(explode(',',$value));
        if(count($list) == 0) return '聘用资格不存在，请重新选择';
        foreach ($list as $v){
            if(!(new OptService())->check_option_exists('cert_employ_qualification',$v)){
                return '聘用资格不存在，请重新选择';
            }
        }
        return true;
    }

    public function check_money($value){
        if(!is_numeric($value)) return '年聘费只能填写数字';
        return true;
    }

    public function chekc_pay_type($value){
        if(!(new OptService())->check_option_exists('cert_employ_pay_type',$value)){
            return '聘费支付方式不存在，请重新选择';
        }
        return true;
    }

    public function check_channel($value){
        if(!(new OptService())->check_option_exists('cert_employ_channel',$value)){
            return '聘用渠道不存在，请重新选择';
        }
        return true;
    }

    public function check_purpose($value){
        if(!(new OptService())->check_option_exists('cert_employ_purpose',$value)){
            return '用途不存在，请重新选择';
        }
        return true;
    }



}

















